#pragma once

#include <stddef.h>
#include "Singleton.h"
#include "SvrList.h"

class CShmMgr;
class CMainSvr;
class CWorkSvr;
class CShmBlock;

template <typename CLock>
class CBufMgr;

typedef CSvrLinkedList<CWorkSvr, CSpinLock> CSvrList;
typedef CSvrList::ListNode SvrNode;

class CSvrMgr : public CSingleton<CSvrMgr>
{
    friend class CSingleton;

public:
    ~CSvrMgr();
    void operator delete(void *ptr);

private:
    CSvrMgr();
    void *operator new(size_t sz);

public:
    static CSvrMgr *Create(int nCore);
    bool Init();
    void Destroy();

    CMainSvr *CreateMainSvr();
    CWorkSvr *CreateWorkSvr();
    void DestroyMainSvr(CMainSvr *pMainSvr);
    void DestroyWorkSvr(CWorkSvr *pWorkSvr);

    int GetListenSocket() { return m_ilfd; }
    CBufMgr<CSpinLock> *GetIpcBufMgr() { return m_pIpcBufMgr; }
    CWorkSvr *GetAcceptServer();

private:
    int m_ilfd;
    CShmMgr *m_pShmMgr;
    CMainSvr *m_pMainSvr;
    CSvrList m_lstWorkSvr;
    CBufMgr<CSpinLock> *m_pIpcBufMgr;
};
